<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item style="margin-left: -25px" label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="处理状态" clearable @change="changeStatus">
          <el-option
            v-for="dict in dict.type.error_handle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          v-if="queryParams.status == '0'"-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="batchHandle('1')"-->
<!--          v-hasPermi="['system:dict:edit']"-->
<!--        >批量处理</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-if="queryParams.status == '1'"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="batchHandle('2')"
          v-hasPermi="['system:dict:remove']"
        >批量删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="errorNoticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        width="120"
        label="异常编码"
        align="center"
        prop="errorNoticeTitle"
      />
      <el-table-column
        label="异常内容"
        align="center"
        prop="noticeContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.error_handle_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="告警时间" align="center" prop="updateTime" width="160">
        <template slot-scope="scope">
           <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listErrorNotice, updateErrorNotice, delErrorNotice, batchHandle } from "@/api/error/error";

export default {
  name: "ErrorNotice",
  dicts: ['error_handle_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常表格数据
      errorNoticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: '0'
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询异常列表 */
    getList() {
      this.loading = true;
      listErrorNotice(this.queryParams).then(response => {
        this.errorNoticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 异常处理状态更新
    handleHandleStatusChange(row) {
      this.$modal.confirm('确认当前异常已处理完成？').then(function () {
        return updateErrorNotice(row.errorNoticeId, '1');
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("处理成功");
      }).catch(function () {
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const errorNoticeId = row.errorNoticeId
      this.$modal.confirm('是否确认删除异常编码为"' + row.errorNoticeTitle + '"的数据项？').then(function() {
        return delErrorNotice(errorNoticeId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    // 切换下拉框选项
    changeStatus(){
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.errorNoticeId)
      this.multiple = !selection.length
    },

    //批量处理、删除
    batchHandle(type){
      if(type == '1'){
        // 批量处理
        const batchReqDto = { type: '1', ids: this.ids};
        this.$modal.confirm('确认当前选中异常已处理完成？').then(function() {
          return batchHandle(batchReqDto);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("处理成功");
        }).catch(() => {});
      }else if(type == '2'){
        // 批量删除
        const batchReqDto = { type: '2', ids: this.ids};
        this.$modal.confirm('确认删除当前选中异常？').then(function() {
          return batchHandle(batchReqDto);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }

  }
};
</script>
